<?php
/**
 * Created by PhpStorm.
 * User: 50223
 * Date: 18.7.26
 * Time: 15:24
 */
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Session;
use App\Models\Auth;
use Illuminate\Support\Facades\DB;
class User_managerController extends Controller
{

      public function user_manager (Request $request){
          $userId = $request->session()->get('userId');
//          return $userId;
          $route_url =  $request->path();

          $Auth = new Auth();
          $Auth->check_auth($userId,$route_url);

        $userAll = DB::table('users')
            ->join('user_roles', 'users.id', '=', 'user_roles.user_id')
            ->select('users.*',  'user_roles.role_name')
            ->orderBy('users.id','desc')->paginate(20);
//        查询所有的角色
          $roleAll  = DB::table('roles')->orderBy('id','asc')->paginate(20);
//        获取第一个角色的id
          $firstId = DB::table('roles')->orderBy('id','asc')->value('id');

          if($request->isMethod('POST')){

              $role_id = $_POST['roleid'];
              $permission = DB::table('role_permissions')->where('role_id','=',$role_id)->value('permission_id');
              $permission = explode(',',$permission);
              $pemisionAll = DB::table('permissions')->where('status','=',1)->pluck('id');
              return array($permission,$pemisionAll);


          }else{
              $permission = DB::table('role_permissions')->where('role_id','=',$firstId)->value('permission_id');
              $permission = explode(',',$permission);

          }


//          获取所有逇权限id

//        查询所有的权限
          $authList = DB::table('permissions')->select('id','title','fid')->groupBy('fid')->get();
          $fid = [1,2,3,4,5,6,7];
          $length = count($fid);
          foreach ($fid as $k=>$v ){

              $perList[$k] = DB::table('permissions')->where('fid','=',$v)->get();
          }



        return view('auth/user_manager',[
            'userAll'=>$userAll,
            'roleAll'=>$roleAll,
            'perList'=>$perList,
            'firstId'=>$firstId,
            'permission'=>$permission
        ]);

    }

      public function user_add(Request $request){

        if($request->isMethod('POST')) {
            $username = $_POST['name'];
            $check = $_POST['check'];

            if($username==''||$check==''){
                return array('status'=>0,'msg'=>'请将信息补充完整');
                exit;
            }
//            先判断用户是否存在
            $res = DB::table('users')->where('username','=',$username)->value('username');
//            return $res;
            if($res==''||is_null($res)){
//               存贮数据（先新增user 在设置关联）
                DB::transaction(function () {
                    $username = $_POST['name'];
                    $check = $_POST['check'];
                    $check = implode(',',$check);

                    DB::table('users')->insert(
                        ['username'=>$username,'password'=>md5('admin123456'),'created_at'=>date('Y-m-d H:i:s',time())
                            ,'updated_at'=>date('Y-m-d H:i:s',time())
                    ]);
                    $userId =  DB::table('users')->where('username','=',$username)->value('id');
                    DB::table('user_roles')->insert([
                        'user_id'=>$userId,'role_id'=>$check,'updated_time'=>date('Y-m-d H:i:s',time())
                        ,'created_time'=>date('Y-m-d H:i:s',time())
                    ]);
                    $check = explode(',',$check);

                   $length = count($check);
                    $checkName=[];
                    for ($i=0;$i<$length;$i++){
                        $name = DB::table('roles')->where('id','=',$check[$i])->where('status','=','1')->value('name');
                        array_push($checkName,$name);

                    }

                    $checkName = implode(',',$checkName);
                    DB::table('user_roles')->where('user_id','=',$userId)->update([ 'role_name'=>$checkName,]);



                });
                return array('status'=>1,'msg'=>'新增用户成功');
            }else{
                return array('status'=>0,'msg'=>'用户已经存在');
            }

        }

    }

      public function  user_delete(Request $request){
          if($request->isMethod('POST')) {

              $allId = $_POST['name'];

              if($allId==''||is_null($allId)){

                  return array('status'=>0,'msg'=>'请选择您要删除的用户');
              }else{
//                  执行删除操作
                  $length = count($allId);
                  for ($i=0;$i<$length;$i++){
                       DB::table('users')->where('id','=',$allId[$i])->delete();
                        DB::table('user_roles')->where('user_id','=',$allId[$i])->delete();

                  }
                  return array('status'=>1,'msg'=>'删除成功');

              }
          }
      }

      public function user_update(Request $request){

          if($request->isMethod('POST')) {
              $username = $_POST['name'];

//              role属性
              $check = $_POST['check'];

              $length = count($check);
              $userid = $_POST['userid'];

              if($userid){
                  //            先判断用户是否存在
                  $res = DB::table('users')->where('id','=',$userid)->value('username');

                  if($res==''||is_null($res)){

                      return array('status'=>0,'msg'=>'该用户存在错误信息');

                  }else{

//                  存贮数据（先新增user 在设置关联）
                      DB::transaction(function () {
                          $username = $_POST['name'];
                          $check = $_POST['check'];
                          $userid = $_POST['userid'];
                          $check = implode(',',$check);

                         $res =  DB::table('users')->where('id','=',$userid)->update(
                              ['username'=>$username, 'updated_at'=>date('Y-m-d H:i:s',time())])
                          ;


                          DB::table('user_roles')->where('user_id','=',$userid)->update([
                              'user_id'=>$userid,'role_id'=>$check,'updated_time'=>date('Y-m-d H:i:s',time())]);

                          $check = explode(',',$check);
                          $length = count($check);
                          $checkName=[];
                          for ($i=0;$i<$length;$i++){
                              $name = DB::table('roles')->where('id','=',$check[$i])->where('status','=','1')->value('name');
                              array_push($checkName,$name);

                          }
                          $checkName = implode(',',$checkName);
                          DB::table('user_roles')->where('user_id','=',$userid)->update([ 'role_name'=>$checkName,]);

                      });
                      return array('status'=>1,'msg'=>'修改用户成功');
                  }

              }else{
                  return array('status'=>0,'msg'=>'请选择需要编辑的用户');
              }
              }
      }

      public function user_info(Request $request){

          if($request->isMethod('POST')) {

              $userid = $_POST['userid'];
              $res = DB::table('users')
                  ->join('user_roles', 'users.id', '=', 'user_roles.user_id')
                  ->where('users.id','=',$userid)
                  ->select('users.username',  'user_roles.role_id')
                  ->first();
              $userinfo = [];
              $username=$res->username;
              $role_id=$res->role_id;
              $role_id = explode(',',$role_id);


              return array('username'=>$username,'role_id'=>$role_id);

          }
      }

      public function role_info(Request $request){

        if($request->isMethod('POST')) {

            $userid = $_POST['userid'];

            $role_info = DB::table('roles')->where('id','=',$userid)->first();
            $role_name = $role_info->name;
            $role_description = $role_info->description;

            return array('role_name'=>$role_name,'role_description'=>$role_description);

        }
    }

      public function password_reset(Request $request){

          if($request->isMethod('POST')) {

              $allId = $_POST['name'];

              if($allId==''||is_null($allId)){

                  return array('status'=>0,'msg'=>'请选择您要重置密码的用户');
              }else{
//                  执行删除操作
                  $length = count($allId);
                  for ($i=0;$i<$length;$i++){
                      DB::table('users')->where('id','=',$allId[$i])->update([ 'password'=>md5('admin12345')]);

                  }
                  return array('status'=>1,'msg'=>'重置密码成功');

              }
          }


      }

      public function role_add(Request $request){

          if($request->isMethod('POST')) {
              $roleinfo = $_POST['Role'];
              $name = $roleinfo['name'];
              $description = $roleinfo['description'];
//              判断该角色是否存在
              $res = DB::table('roles')->where('name','=',$name)->get();
              if($res==''||is_null($res)){
                  return array('status'=>'0','msg'=>'该角色已经存在');
              }else{
                  $res = DB::table('roles')->insert(['name'=>$name,'description'=>$description,'updated_time'=>date('Y-m-d H:i:s',time())
                      ,'created_time'=>date('Y-m-d H:i:s',time())]);
                  if($res){
                      return array('status'=>'1','msg'=>'角色新增成功');
                  }
              }
          }

      }

      public function role_delete(Request $request){
          if($request->isMethod('POST')) {

              $allId = $_POST['name'];
              if($allId==''||is_null($allId)){

                  return array('status'=>0,'msg'=>'请选择您要删除的用户');
              }else{
//                  执行删除操作
                  $length = count($allId);
                  for ($i=0;$i<$length;$i++){

                      DB::table('roles')->where('id','=',$allId[$i])->delete();


                  }
                  return array('status'=>1,'msg'=>'删除成功');

              }

          }
      }

      public function role_update(Request $request){

        if($request->isMethod('POST')){

            $name = $_POST['name'];

            $description = $_POST['description'];
            $userid = $_POST['userid'];
            if($userid){
                $res = DB::table('roles')->where('id','=',$userid)->update(['name'=>$name,'description'=>$description,'updated_time'=>date('Y-m-d H:i:s',time())]);
                if($res){
                    return array('status'=>1,'msg'=>'角色信息修改成功');
                }
            }else{
                return array('status'=>0,'msg'=>'该角色存在错误信息');
            }

        }

      }

      public function role_permissions(Request $request){

          if($request->isMethod('POST')){

              $permissionId = $_POST['name'];
              $roleId = $_POST['roleid'];
              $permissionId = implode(',',$permissionId);

//            将数据增加到roles表中
              $roleid_have =   DB::table('role_permissions')->where('role_id','=',$roleId)->value('id');

              if($roleid_have){
                  $res = DB::table('role_permissions')->where('id','=',$roleid_have)->update(['role_id'=>$roleId,'permission_id'=>$permissionId,'created_time'=>date('Y-m-d H:i:s',time())]);
                  if($res){
                      return array('status'=>1,'msg'=>'权限更新成功');

                  }else{

                      return array('status'=>0,'msg'=>'权限更新失败');
                  }
              }else{
                  $res = DB::table('role_permissions')->insert(['role_id'=>$roleId,'permission_id'=>$permissionId,'created_time'=>date('Y-m-d H:i:s',time())]);
                  if($res){
                      return array('status'=>1,'msg'=>'权限更新成功');

                  }else{

                      return array('status'=>0,'msg'=>'权限更新失败');
                  }
              }




          }else{
              return array('status'=>0,'msg'=>'禁止非法访问');
          }
      }

      public function user_screen(Request $request){

          if($request->isMethod('POST')){


              $user_name = trim($_POST['user_name']);
              if($user_name){
                  $userinfo = DB::table('users')
                      ->join('user_roles', 'users.id', '=', 'user_roles.user_id')
                      ->select('users.*',  'user_roles.role_name')
                      ->where('users.username','like','%'.$user_name.'%')
                      ->orderBy('users.id','desc')->paginate(20);

                 if($userinfo->count() !=0){
                     return array('status'=>1,'msg'=>'共查询到'.$userinfo->count().'条结果','userinfo'=>$userinfo);
                 }else{
                     return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');
                 }

              }
          }
          }
      public function role_screen(Request $request){

        if($request->isMethod('POST')){
            $role_name = $_POST['role_name'];
            if($role_name){
                $userinfo = DB::table('users')
                    ->join('user_roles', 'users.id', '=', 'user_roles.user_id')
                    ->select('users.*',  'user_roles.role_name')
                    ->where('user_roles.role_name','like','%'.$role_name.'%')
                    ->orderBy('users.id','desc')->paginate(20);

                if($userinfo->count() !=0){
                    return array('status'=>1,'msg'=>'共查询到'.$userinfo->count().'条结果','userinfo'=>$userinfo);
                }else{
                    return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');
                }

            }
        }
    }
        public function time_screen(Request $request){

        if($request->isMethod('POST')){
            $beginTime = $_POST['beginTime'];
            $endTime = $_POST['endTime'];

            if($beginTime&&$endTime){
                $userinfo = DB::table('users')
                    ->join('user_roles', 'users.id', '=', 'user_roles.user_id')
                    ->select('users.*',  'user_roles.role_name')
                    ->whereBetween('users.created_at',[$beginTime,$endTime])
                    ->orderBy('users.id','desc')->paginate(20);

                if($userinfo->count() !=0){
                    return array('status'=>1,'msg'=>'共查询到'.$userinfo->count().'条结果','userinfo'=>$userinfo);
                }else{
                    return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');
                }

            }
        }
    }
//以后价格登录时间继路
    public function login_time_screen(Request $request){

        if($request->isMethod('POST')){
            $loginLatyTime = $_POST['loginLatyTime'];
            $loginOverTime = $_POST['loginOverTime'];

            if($loginOverTime&&$loginLatyTime){
                $userinfo = DB::table('users')
                    ->join('user_roles', 'users.id', '=', 'user_roles.user_id')
                    ->select('users.*',  'user_roles.role_name')
                    ->whereBetween('users.updated_at',[$loginLatyTime,$loginOverTime])
                    ->orderBy('users.id','desc')->paginate(20);

                if($userinfo->count() !=0){
                    return array('status'=>1,'msg'=>'共查询到'.$userinfo->count().'条结果','userinfo'=>$userinfo);
                }else{
                    return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');
                }

            }
        }
    }
    public function user_type_screen(Request $request){

        if($request->isMethod('POST')){

            $status = $_POST['status'];

            if($status==1||$status==0){

                $userinfo = DB::table('users')
                    ->join('user_roles', 'users.id', '=', 'user_roles.user_id')
                    ->select('users.*',  'user_roles.role_name')
                    ->where('users.status','=',$status)
                    ->orderBy('id','desc')->paginate(20);

                if($userinfo->count() !=0){
                    return array('status'=>1,'msg'=>'共查询到'.$userinfo->count().'条结果','userinfo'=>$userinfo);
                }else{
                    return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据');
                }

            }
        }


    }
    public function update_user_status(Request $request){

        if($request->isMethod('POST')){

            $id = $_POST['id'];
            $status = $_POST['status'];

            if($id){
                if($status=='1'){

                    $userinfo = DB::table('users')
                        ->where('id','=',$id)
                        ->update(['status'=>0]);
                }else{
                    $userinfo = DB::table('users')
                        ->where('id','=',$id)
                        ->update(['status'=>1]);
                }


                if($userinfo){
                    return array('status'=>1,'msg'=>'状态信息修改成功');
                }else{
                    return array('status'=>0,'msg'=>'状态信息修改失败');
                }

            }else{
                return array('status'=>0,'msg'=>'状态信息修改失败');
            }
        }


    }

    public function role_manager_screen(Request $request){

        if($request->isMethod('POST')){

            $role_id = $_POST['role_id'];

            if($role_id){
                $userinfo=DB::table('roles')
                    ->where('id','=',$role_id)
                    ->orderBy('id','desc')->paginate(20);

                if($userinfo->count() !=0){
                    return array('status'=>1,'msg'=>'共查询到'.$userinfo->count().'条结果','userinfo'=>$userinfo);
                }else{
                    return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');
                }


            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');
            }
        }

    }

    public function serch_role_description(Request $request){
        if($request->isMethod('POST')){

            $role_description =trim($_POST['role_description']);

            if($role_description){
                $userinfo=DB::table('roles')
                    ->where('description','like','%'.$role_description.'%')
                    ->orderBy('id','desc')->paginate(20);

                if($userinfo->count() !=0){
                    return array('status'=>1,'msg'=>'共查询到'.$userinfo->count().'条结果','userinfo'=>$userinfo);
                }else{
                    return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据',);
                }


            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');
            }
        }
    }

    public function role_time_screen(Request $request){
        if($request->isMethod('POST')){
            $role_beginTime = $_POST['role_beginTime'];
            $role_endTime = $_POST['role_endTime'];

            if($role_beginTime&&$role_endTime){
                $userinfo = DB::table('roles')
                    ->whereBetween('created_time',[$role_beginTime,$role_endTime])
                    ->orderBy('id','desc')->paginate(20);

                if($userinfo->count() !=0){
                    return array('status'=>1,'msg'=>'共查询到'.$userinfo->count().'条结果','userinfo'=>$userinfo);
                }else{
                    return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');
                }

            }else{
                return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据','userinfo'=>'');
            }
        }

    }

    public function role_status_screen(Request $request){

        if($request->isMethod('POST')){

            $status = $_POST['status'];

            if($status==1||$status==0){

                $userinfo = DB::table('roles')

                    ->where('status','=',$status)
                    ->orderBy('id','desc')->paginate(20);

                if($userinfo->count() !=0){
                    return array('status'=>1,'msg'=>'共查询到'.$userinfo->count().'条结果','userinfo'=>$userinfo);
                }else{
                    return array('status'=>0,'msg'=>'很抱歉,未查询到匹配数据');
                }

            }
        }
    }

    public function update_role_status(Request $request){

        if($request->isMethod('POST')){

            $id = $_POST['id'];
            $status = $_POST['status'];

            if($id){
                if($status=='1'){

                    $userinfo = DB::table('roles')
                        ->where('id','=',$id)
                        ->update(['status'=>0]);
                }else{
                    $userinfo = DB::table('roles')
                        ->where('id','=',$id)
                        ->update(['status'=>1]);
                }


                if($userinfo){
                    return array('status'=>1,'msg'=>'状态信息修改成功');
                }else{
                    return array('status'=>0,'msg'=>'状态信息修改失败');
                }

            }else{
                return array('status'=>0,'msg'=>'状态信息修改失败');
            }
        }


    }


 }
